/**
 * author wen
 * cunyu
 * email wjd_13@hotmail.com
 */
package com.cunyu.action;

import com.cunyu.bean.Form;
import com.cunyu.bean.Muser;
import com.cunyu.bean.ReqData;
import com.cunyu.bean.RespData;
import com.cunyu.listener.RequestContext;
import com.cunyu.service.OrderService;
import com.cunyu.service.PushService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 订单查看
 * @author win
 * 2019/04/28-15:18
 */
@Controller
public class OrderController extends BaseController{

    @Resource
    PushService pushService ;

    @Resource
    OrderService orderService ;

    @RequestMapping("/order/page")
    public String page(Model model, ReqData reqData, Form form){
        Cookie cookie = RequestContext.get().getCookie("token");
        Muser u = (Muser)timedCache.get(cookie.getValue());
        RespData<List<Map<String, Object>>> listRespData = orderService.searchPage(reqData,form,u);
        model.addAttribute("respData",listRespData) ;
        model.addAttribute("form",form) ;
        return "order/page" ;
    }

    @RequestMapping("/order/refund")
    public void refund(HttpServletResponse res ,int id) {
        Cookie cookie = RequestContext.get().getCookie("token");
        Muser u = (Muser)timedCache.get(cookie.getValue());
        int r = orderService.refund(u, id);

        //if(u.getRole())
        if(r == 1){
            int uid = orderService.getTheSuserId(id) ;
            pushService.push("退单申请审核","您的退单请求已通过",uid);

            printOK(res,"success",1);
        }else{
            printError(res,"退单失败，请联系管理员或稍后重试。"+r);
        }
    }

    /**
     * 拒绝退款。
     */
    @RequestMapping("/order/refuse")
    public void refuse(HttpServletResponse res ,int id) {
        Cookie cookie = RequestContext.get().getCookie("token");
        Muser u = (Muser)timedCache.get(cookie.getValue());
        int r = orderService.refuse(u, id);
        if(r == 1){

            int uid = orderService.getTheSuserId(id) ;
            pushService.push("退单申请审核","您的退单请求被拒绝",uid);

            printOK(res,"success",1);
        }else{
            printError(res,"数据处理失败，请联系管理员或稍后重试。"+r);
        }
    }

}
